﻿var Mullivan;
if (!Mullivan) Mullivan = {};
else if (typeof Mullivan != "object")
    throw new Error("Mullivan already exists and is not an object");

if (Mullivan.Utilities)
    throw new Error("Mullivan.Utilities already exists");

Mullivan.Utilities = {
    HtmlEncode: function(textToEncode) {
        var result = textToEncode;

        var amp = "&";
        var gt = ">";
        var lt = "<";
        var quot = "\"";
        var apos = "'";
        var nbsp = " ";

        var html_gt = "&gt;";
        var html_lt = "&lt;";
        var html_amp = "&amp;";
        var html_quot = "&quot;";
        var html_apos = "&apos;";
        var html_nbsp = "&nbsp;";

        while (result.indexOf(amp) > -1)
            result = result.replace(amp, html_amp);
        while (result.indexOf(quot) > -1)
            result = result.replace(quot, html_quot);
        while (result.indexOf(lt) > -1)
            result = result.replace(lt, html_lt);
        while (result.indexOf(gt) > -1)
            result = result.replace(gt, html_gt);
        while (result.indexOf(apos) > -1)
            result = result.replace(apos, html_apos);
        while (result.indexOf(nbsp) > -1)
            result = result.replace(nbsp, html_nbsp);

        return result;
    },

    HtmlDecode: function(textToDecode) {
        var result = textToDecode;

        var gt = "&gt;";
        var lt = "&lt;";
        var amp = "&amp;";
        var quot = "&quot;";
        var apos = "&apos;";
        var nbsp = "&nbsp;";

        var html_gt = ">";
        var html_lt = "<";
        var html_amp = "&";
        var html_quot = "\"";
        var html_apos = "'";
        var html_nbsp = " ";

        while (result.indexOf(amp) > -1)
            result = result.replace(amp, html_amp);
        while (result.indexOf(quot) > -1)
            result = result.replace(quot, html_quot);
        while (result.indexOf(lt) > -1)
            result = result.replace(lt, html_lt);
        while (result.indexOf(gt) > -1)
            result = result.replace(gt, html_gt);
        while (result.indexOf(apos) > -1)
            result = result.replace(apos, html_apos);
        while (result.indexOf(nbsp) > -1)
            result = result.replace(nbsp, html_nbsp);

        return result;
    },

    HtmlEncode2: function(textToEncode) {
        var result = "";
        for (var i = 0; i < textToEncode.length; i++) {
            var currentCharCode = textToEncode.charCodeAt(i);
            result += "&#" + currentCharCode + ";";
        }
        return result;
    },

    HtmlDecode2: function(textToDecode) {
        var result = "";

        if (textToDecode.indexOf("&") < 0) {
            result = textToDecode;
        }
        else {
            for (var i = 0; i < textToDecode.length; i++) {
                var charCurrent = textToDecode.charAt(i);
                var currentEntity = "";
                if (charCurrent == "&") {
                    var endIndex = textToDecode.indexOf(";", i + 1);
                    if (endIndex > 0) {
                        var entity = textToDecode.substring(i + 1, endIndex);
                        if ((entity.length > 1) && (entity.charAt(0) == "#")) {
                            try {
                                if ((entity.charAt(1) == "x") || (entity.charAt(1) == "X")) {
                                    currentEntity = String.fromCharCode(entity.substring(2));
                                }
                                else {
                                    currentEntity = String.fromCharCode(entity.substring(1));
                                }
                            }
                            catch (e) {
                                i++;
                            }
                        }
                    }
                }
                result += currentEntity;
            }
        }

        return this.HtmlDecode(result);
    },

    UrlEncode: function(textToEncode) {
        return escape(textToEncode).replace(/\+/g, '%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
    },

    UrlDecode: function(textToDecode) {
        var result = "";

        if (textToDecode.indexOf("%") < 0) {
            result = textToDecode;
        }
        else {
            for (var i = 0; i < textToDecode.length; i++) {
                var charCurrent = textToDecode.charAt(i);
                var currentEntity = "";
                if (charCurrent == "%") {
                    var endIndex = textToDecode.indexOf("%", i + 1);
                    if (endIndex < 0) {
                        endIndex = textToDecode.length;
                    }
                    var entity = textToDecode.substring(i + 1, endIndex);
                    currentEntity = String.fromCharCode(parseInt(entity, 16));
                    i += entity.length;
                }
                result += currentEntity;
            }
        }
        return result;
    },

    XmlEncode: function(textToEncode) {
        var result = textToEncode;

        var amp = "&";
        var gt = ">";
        var lt = "<";
        var quot = "\"";
        var apos = "'";

        var xml_gt = "&gt;";
        var xml_lt = "&lt;";
        var xml_amp = "&amp;";
        var xml_quot = "&quot;";
        var xml_apos = "&apos;";

        while (result.indexOf(amp) > -1)
            result = result.replace(amp, xml_amp);
        while (result.indexOf(quot) > -1)
            result = result.replace(quot, xml_quot);
        while (result.indexOf(lt) > -1)
            result = result.replace(lt, xml_lt);
        while (result.indexOf(gt) > -1)
            result = result.replace(gt, xml_gt);
        while (result.indexOf(apos) > -1)
            result = result.replace(apos, xml_apos);

        return result;
    },

    XmlDecode: function(textToDecode) {
        var result = textToDecode;

        var gt = "&gt;";
        var lt = "&lt;";
        var amp = "&amp;";
        var quot = "&quot;";
        var apos = "&apos;";

        var xml_gt = ">";
        var xml_lt = "<";
        var xml_amp = "&";
        var xml_quot = "\"";
        var xml_apos = "'";

        while (result.indexOf(amp) > -1)
            result = result.replace(amp, xml_amp);
        while (result.indexOf(quot) > -1)
            result = result.replace(quot, xml_quot);
        while (result.indexOf(lt) > -1)
            result = result.replace(lt, xml_lt);
        while (result.indexOf(gt) > -1)
            result = result.replace(gt, xml_gt);
        while (result.indexOf(apos) > -1)
            result = result.replace(apos, xml_apos);

        return result;
    },

    NewGuid: function() {
        var result, i, j;
        result = '';
        for (j = 0; j < 32; j++) {
            if (j == 8 || j == 12 || j == 16 || j == 20)
                result = result + '-';
            i = Math.floor(Math.random() * 16).toString(16).toUpperCase();
            result = result + i;
        }
        return result
    }
};